package com.compile;

import com.annotion.Register;
import com.google.auto.service.AutoService;
import com.squareup.javapoet.ClassName;
import com.squareup.javapoet.JavaFile;
import com.squareup.javapoet.MethodSpec;
import com.squareup.javapoet.TypeName;
import com.squareup.javapoet.TypeSpec;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.processing.AbstractProcessor;
import javax.annotation.processing.Filer;
import javax.annotation.processing.ProcessingEnvironment;
import javax.annotation.processing.Processor;
import javax.annotation.processing.RoundEnvironment;
import javax.lang.model.SourceVersion;
import javax.lang.model.element.Element;
import javax.lang.model.element.ElementKind;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
import javax.lang.model.element.TypeElement;
import javax.lang.model.util.Elements;

/**
 * Created by huilin on 2017/6/15.
 */
@AutoService(Processor.class)
public class RouteProcesser extends AbstractProcessor {
    private Elements elements;
    private Filer filer;

    @Override
    public synchronized void init(ProcessingEnvironment processingEnv) {
        super.init(processingEnv);
        filer = processingEnv.getFiler();
        elements = processingEnv.getElementUtils();
    }

    @Override
    public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
        processRoute(roundEnv);
        return true;
    }

    private void processRoute(RoundEnvironment roundEnv) {

        Set<? extends Element> elementsAnnotatedWith = roundEnv.getElementsAnnotatedWith(Register.class);
        MethodSpec.Builder builder = MethodSpec.methodBuilder("register")
                .addModifiers(Modifier.PUBLIC,Modifier.STATIC)
                .returns(TypeName.VOID);
        for (Element element : elementsAnnotatedWith){
            if(element.getKind() != ElementKind.CLASS){
                throw new IllegalArgumentException("route annotion is not class");
            }
            Name simpleName = element.getSimpleName();
            Register annotation = element.getAnnotation(Register.class);
            String value = annotation.value();
//            Router.route(ActivityRule.ACTIVITY_SCHEME + "module_b", ModuleBActivity.class);
            builder.addStatement("$T.route($S,$T.class)", ClassName.get("xtu.www.common.router","Router"),value,element.asType());
        }
        TypeSpec typeSpec = TypeSpec.classBuilder("Route_Register")
                .addModifiers(Modifier.PUBLIC)
                .addMethod(builder.build())
                .build();
        try {
            JavaFile.builder("xtu.mo",typeSpec).build().writeTo(filer);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 指定使用的java版本
     *
     * @return
     */
    @Override
    public SourceVersion getSupportedSourceVersion() {
        return SourceVersion.latestSupported();
    }

    @Override
    public Set<String> getSupportedAnnotationTypes() {
        Set<String> stringSet = new HashSet<>();
        stringSet.add(Register.class.getCanonicalName());
        return stringSet;
    }

}
